/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kernel;

import gui.Factory;
import gui.manageplugins.Download;
import java.awt.TrayIcon;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;
import javax.swing.JInternalFrame;
import org.jdom.Element;

/**
 *
 * @author alvaro
 */
public class PluginOperations {

    public static JInternalFrame activatePlugin(String module, String plugin) throws Exception {
        ActivatePlugin activ = new ActivatePlugin(module, plugin);
        return activ.getJInternalFrame();
    }

    public static String removePlugin(String module, String plugin){
        String result =  RemovePlugin.remove(module, plugin);
        Factory.makeManagePluginsSingleton().reloadTree();
        Factory.makeGUISingleton().loadPluginsIcons();
        Factory.makeGUISingleton().modulesMenu();
        return result;
    }

    public static void cleanFolder(String folder){
        RemovePlugin.cleanFolder(folder);
    }

    public static void addPlugin(){

    }

    public static Vector<Plugin> getInstalledPluginsByModule(String module){
        return ListInstalledPlugins.installedPluginsByModule(module);
    }

    public static Vector<Plugin> getAllInstalledPlugins(){
        return ListInstalledPlugins.allInstalledPlugins();
    }

    public static void createModule(String moduleName){
        CreateModule.create(moduleName);

        //new module trayicon message
        XMLPluginConfiguration xml = new XMLPluginConfiguration(Factory.getWorkspace()+File.separator+
                "files"+File.separator+"language.xml");

        String lang = xml.getDoc().getRootElement().getAttributeValue("selected");
        String title = xml.getTag("/languages/"+lang+"/windows/gui/module_created_title");
        String message = xml.getTag("/languages/"+lang+"/windows/gui/module_created_message");
        message = message.replace("{module_name}", "\""+moduleName+"\"");

        Factory.makeGUISingleton().showTrayIconMessage(title, message, TrayIcon.MessageType.INFO);
    }

    public static Vector<Module> getAllModules(){
        Vector<Module> vec = ListModules.getModules();
        for(Module mod : vec){
            Vector<Plugin> plugs = getInstalledPluginsByModule(mod.getName());
            for(Plugin plug : plugs){
                mod.addChild(plug);
            }
        }

        return vec;
    }

    public static void movePluginInsideModuleTo(String plugin, String module, int pos){
        XMLPluginConfiguration xml = new XMLPluginConfiguration(Factory.getWorkspace()+File.separator+
                "modules"+File.separator+module+File.separator+"plugins.xml");
        List l = xml.getDoc().getRootElement().getChildren();
        Element root = xml.getDoc().getRootElement();
        int i = 0;
        Element e = null;
        for(i = 0;i<l.size();i++){
            e = (Element) l.get(i);
            System.out.println("Name: "+e.getAttributeValue("name"));
            if(e.getAttributeValue("name").equals(plugin)){
                break;
            }
        }
        if(i == l.size() || e == null)
            return;

        Vector<Element> vec = new Vector<Element>();

        root.removeContent(e);
        List o = root.getChildren();

        //copy to new vector
        for(i = 0;i<o.size();i++){
            vec.add((Element) l.get(i));
        }
        //remove all content
        root.removeContent();

        //add old content
        for(i = 0;i<vec.size();i++){
                root.addContent(vec.get(i));
        }

        //add at position
        if(root.getChildren().size() <= pos){
            //add at end
            root.addContent(e);
        }
        else{
            root.addContent(pos, e);
        }

        //write data
        xml.getDoc().setRootElement(root);
        xml.dumpFile(Factory.getWorkspace()+File.separator+
                "modules"+File.separator+module+File.separator+"plugins.xml");

        Factory.makeManagePluginsSingleton().reloadTree();
        Factory.makeGUISingleton().loadPluginsIcons();
        Factory.makeGUISingleton().modulesMenu();

    }

    public static void movePluginToModule(String pluginName, String modSource, String modDest) throws IOException{
        copyPluginToModule(pluginName, modSource, modDest);
        removePlugin(modSource, pluginName);
        Factory.makeManagePluginsSingleton().reloadTree();
        Factory.makeGUISingleton().loadPluginsIcons();
        Factory.makeGUISingleton().modulesMenu();
    }

    public static void copyPluginToModule(String pluginName, String modSource, String modDest) throws IOException{
        CopyPlugin.copyPlugin(pluginName, modSource, modDest);
        Factory.makeGUISingleton().loadPluginsIcons();
    }

    public static void removeModule(String moduleName){
        Vector<Plugin> plugins = getInstalledPluginsByModule(moduleName);
        if(plugins.size() > 0)
            for(Plugin plug : plugins){
                removePlugin(moduleName, plug.getName());
            }
        else{
            String s = File.separator;
            XMLPluginConfiguration xml = new XMLPluginConfiguration(Factory.getWorkspace()+s+"files"+s+"modules.xml");
            String folderToRemove = xml.getTag("/modules/module[@name=\""+moduleName+"\"]/folder");
            folderToRemove = Factory.getWorkspace()+s+folderToRemove;
            //remove from xml
            Element root = xml.getDoc().getRootElement();
            List l = root.getChildren();
            for(int i = 0;i<l.size();i++){
                Element e = (Element) l.get(i);
                if(e.getAttributeValue("name").equals(moduleName)){
                    l.remove(i);
                    root.setContent(l);
                    break;
                }
            }
            //dump file
            xml.getDoc().setRootElement(root);
            xml.dumpFile(Factory.getWorkspace()+s+"files"+s+"modules.xml");
            RemovePlugin.cleanFolder(folderToRemove);
        }
        Factory.makeManagePluginsSingleton().reloadTree();
        Factory.makeGUISingleton().loadPluginsIcons();
        Factory.makeGUISingleton().modulesMenu();
    }

    /*public static InstallRepositoryPlugin installRepositoryPlugin(Plugin p, Download d){
        InstallRepositoryPlugin install = new InstallRepositoryPlugin(p, d);
        return install;
    }*/

    public static Vector<Plugin> getRepositoryPlugins() throws Exception {
        return kernel.net.ListRepositoryPlugins.getRepositoryPlugins();
    }

    public static void main(String args[]){
        Factory.setWorkspace(".");
        kernel.PluginOperations.movePluginInsideModuleTo("LevelGenerator", "Regnum Online", 1);
    }

}
